package com.finace.money.presenter;

import android.text.SpannableString;

import com.finace.money.R;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.common.util.chartutil.ColorUtil;
import com.finace.money.data.bean.DealInfoBean;
import com.finace.money.data.bean.ExtractTickBean;
import com.finace.money.data.bean.HistoryLineBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.data.bean.chartbean.CMinute;
import com.finace.money.data.bean.chartbean.FenshiParam;
import com.finace.money.data.bean.chartbean.MinuteDataBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangpeng on 2018/1/15.
 */

public class MinutesViewPresenter extends BasePresenter {

    public MinutesViewPresenter() {
        super();
    }

    public MinuteDataBean getData(ArrayList<HistoryLineBean> lineList, double mPreSettle) {

        MinuteDataBean minuteDataBean = new MinuteDataBean();
        List<CMinute> cMinutes = new ArrayList<>();
        double average = 0;
        for (int i = 0; i < lineList.size(); i++) {
            HistoryLineBean.MBodyBean m_body = lineList.get(i).getM_Body();
            double zhangdie = 0;
            if (i != 0) {
                zhangdie = m_body.getM_Close().getM_Value() - mPreSettle;
            }
            average += m_body.getM_Close().getM_Value();

            cMinutes.add(new CMinute(m_body.getM_EndTime(),
                    m_body.getM_Close().getM_Value(),
                    m_body.getM_Volume(),
//                    m_body.getM_AveragePrice().getM_Value(),//值不对，重新自己算
                    average / (i + 1),
                    zhangdie,
                    m_body.getM_Money().getM_Value()));
        }
        minuteDataBean.setData(cMinutes);
        HistoryLineBean bean = lineList.get(lineList.size() - 1);
        HistoryLineBean.MBodyBean mBody = bean.getM_Body();
        minuteDataBean.setParam(new FenshiParam(mPreSettle, "21:00-23:30|9:00-11:30|13:00-15:00", 0, mBody.getM_EndTime()));
        return minuteDataBean;
    }

    public List<DealInfoBean> getDealInfoData(List<HistoryLineBean> mHistoryList, TickBean tickBean) {


        List<DealInfoBean> dealInfoBeans = new ArrayList<>();
        if (null != tickBean) {
            String aveStr = "-";
            if (null != mHistoryList && mHistoryList.size() > 0) {
                double allPrice = 0;
                for (HistoryLineBean bean : mHistoryList) {
                    allPrice += bean.getM_Body().getM_Close().getM_Value();
                }
                if (allPrice > 0) {
                    aveStr = StringUtil.getDecimal((allPrice / mHistoryList.size()));
                }

            }
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.slae_price), tickBean.getAsk()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.sale_price), tickBean.getAsksize()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.news), tickBean.getTrade()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.buy_price), tickBean.getBid()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.buy_size), tickBean.getBidsize()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.open_pan), String.valueOf(tickBean.getOpen())));
            double zhangDie = (Double.valueOf(tickBean.getTrade()) - Double.valueOf(tickBean.getPresettle())) / Double.valueOf(tickBean.getPresettle());
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.zhang_die), StringUtil.getNumberFormat(String.valueOf(zhangDie))));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.highest), tickBean.getHigh()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.chi_cang_size), tickBean.getOi()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.deal_count_have), tickBean.getVol()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.lowest), tickBean.getLow()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.day_add_cang), tickBean.getChange()));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.price_ava), aveStr));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.presettle), tickBean.getPresettle()));
            double zhangTing = Double.valueOf(tickBean.getPresettle()) * 1.1;
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.zhang_ting), StringUtil.getNumberFormat(String.valueOf(zhangTing))));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.settlement), "-"));
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.pre_buy), "-"));
            double dieTing = Double.valueOf(tickBean.getPresettle()) * 0.9;
            dealInfoBeans.add(new DealInfoBean(UIUtil.getString(R.string.die_ting), StringUtil.getNumberFormat(String.valueOf(dieTing))));
        }
        return dealInfoBeans;
    }


    public ExtractTickBean extractData(TickBean payloadBean, double mPreSettle, MinuteDataBean data) {

        double zhangdie = Double.valueOf(payloadBean.getTrade()) - mPreSettle;
        String high = UIUtil.getString(R.string.high) + payloadBean.getHigh();
        SpannableString highStr = null;
        if (Double.valueOf(payloadBean.getHigh()) > mPreSettle) {
            highStr = StringUtil.getSpannableString(high, 0, UIUtil.getString(R.string.high).length(), R.style.text_White899198_12, R.style.text_RedFF3B30_12);
        } else {
            highStr = StringUtil.getSpannableString(high, 0, UIUtil.getString(R.string.high).length(), R.style.text_White899198_12, R.style.text_Green4CD964_12);
        }
        String low = UIUtil.getString(R.string.low) + payloadBean.getLow();
        SpannableString lowStr = null;
        if (Double.valueOf(payloadBean.getLow()) > mPreSettle) {
            lowStr = StringUtil.getSpannableString(low, 0, UIUtil.getString(R.string.low).length(), R.style.text_White899198_12, R.style.text_RedFF3B30_12);
        } else {
            lowStr = StringUtil.getSpannableString(low, 0, UIUtil.getString(R.string.low).length(), R.style.text_White899198_12, R.style.text_Green4CD964_12);
        }

        String pre = UIUtil.getString(R.string.presettle) + payloadBean.getPresettle();
        SpannableString preStr = StringUtil.getSpannableString(pre, 0, UIUtil.getString(R.string.presettle).length(), R.style.text_White899198_12, R.style.text_white_12);

        String oi = UIUtil.getString(R.string.chi_cang) + payloadBean.getOi();
        SpannableString oiStr = StringUtil.getSpannableString(oi, 0, UIUtil.getString(R.string.presettle).length(), R.style.text_White899198_12, R.style.text_white_12);

        String size = UIUtil.getString(R.string.size) + payloadBean.getVol();
        SpannableString sizeStr = StringUtil.getSpannableString(size, 0, UIUtil.getString(R.string.size).length(), R.style.text_White899198_12, R.style.text_white_12);

        String change = UIUtil.getString(R.string.day_add) + payloadBean.getChange();
        SpannableString changeStr = StringUtil.getSpannableString(change, 0, UIUtil.getString(R.string.day_add).length(), R.style.text_White899198_12, R.style.text_white_12);
        double average;
        if (null == data) {
            average = 0;
        } else {
            average = data.getData().get(data.getData().size() - 1).getAverage();
        }
        String info = ColorUtil.getCurPriceInfo(new CMinute(0, Double.valueOf(payloadBean.getTrade()), 0,
                average,
                zhangdie, 0), mPreSettle);

        ExtractTickBean extractTickBean = new ExtractTickBean(payloadBean.getTrade(),
                zhangdie + "  " + StringUtil.getNumberFormat(String.valueOf(zhangdie / mPreSettle)),
                highStr,
                preStr,
                oiStr,
                lowStr,
                sizeStr,
                changeStr);

        extractTickBean.setInfo(info);

        return extractTickBean;
    }
}
